package com.edocbase.desktop.plugins.mdiary.figures;

import org.eclipse.draw2d.AbstractLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

class DiaryLayout extends AbstractLayout {
	private IFigure titleFigure,descFigure,thumbFigure,toolbarFigure=null;
	
	static final String LAYOUT_TITLE = "title";
	static final String LAYOUT_DESC = "desc";
	static final String LAYOUT_THUMB = "thumb";
	static final String LAYOUT_TOOLBAR = "toolbar";
	
	@Override
	public void setConstraint(IFigure child, Object constraint) {
		if (constraint.equals(LAYOUT_TITLE)){
			titleFigure = child;
		}else if (constraint.equals(LAYOUT_DESC)){
			descFigure = child;
		}else if(constraint.equals(LAYOUT_THUMB)){
			thumbFigure = child;
		}else if (constraint.equals(LAYOUT_TOOLBAR)){
			toolbarFigure = child;
		}
		super.setConstraint(child, constraint);
	}

	@Override
	public void layout(IFigure container) {
		Rectangle rect = container.getClientArea();
		DiaryFigure df = (DiaryFigure)container;
		//Rectangle maxRect =((Viewport)df.getRoot().getParent()).getClientArea();
		//System.out.println(maxRect);
		int x = rect.x;
		int y = rect.y;
		
		int w =rect.width;//Math.max(maxRect.width,rect.width);
		
		if (thumbFigure != null){
			Dimension thumbSize = thumbFigure.getPreferredSize();
			w = w - thumbSize.width;
			thumbFigure.setBounds(new Rectangle(x+w,y,thumbSize.width,thumbSize.height));
		}
		if (titleFigure != null){
			Dimension size = titleFigure.getPreferredSize();
			titleFigure.setBounds(new Rectangle(x,y,w,size.height));
			y = y + size.height;
		}
		if (descFigure != null){
			Dimension size = descFigure.getPreferredSize();
			descFigure.setBounds(new Rectangle(x,y,w,size.height));
			y = y + size.height;
		}
		if (toolbarFigure != null){
			Dimension size = toolbarFigure.getPreferredSize();
			toolbarFigure.setBounds(new Rectangle(x,y,w,size.height));
			y = y + size.height;
		}
		
	}

	@Override
	protected Dimension calculatePreferredSize(IFigure container, int wHint,
			int hHint) {
		Dimension size = new Dimension(0,0);
		
		if (titleFigure != null){
			Dimension dsize = titleFigure.getPreferredSize(wHint, hHint);
			size.expand(dsize);
			Insets insets = titleFigure.getInsets();
			size = size.expand(insets.getWidth()*2,insets.getHeight()*2);
		}
		if (descFigure != null){
			Dimension dsize = descFigure.getPreferredSize(wHint, hHint);
			size = size.expand(0,dsize.height);
			
			size.width = Math.max(dsize.width, size.width);
		}
		if (toolbarFigure != null){
			Dimension dsize = toolbarFigure.getPreferredSize(wHint, hHint);
			size = size.expand(0,dsize.height);
		}
		if (thumbFigure != null){
			Dimension dsize = thumbFigure.getPreferredSize(wHint, hHint);
			size = size.expand(dsize.width,0);
		}	
		//return new Dimension(151,52);
		return size;
	}

}
